package com.jianying.day01;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink04_WC_Stream_UnBounded_Generic
 * Package: com.atguigu.day01
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/3/29 14:22
 * @Version: 1.0
 */
public class Flink04_WC_Stream_UnBounded_Generic {
    public static void main(String[] args) throws Exception {
        // TODO 简化
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSink<Tuple2<String, Long>> print = env.socketTextStream("hadoop102", 8888)
//                        .flatMap(
//                                new FlatMapFunction<String, Tuple2<String, Long>>() {
//                                    @Override
//                                    public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
//                                        String[] words = s.split(" ");
//                                        for (String word : words) {
//                                            collector.collect(Tuple2.of(word, 1L));
//                                        }
//                                    }
//                                }
//                        )
                .flatMap((FlatMapFunction<String, Tuple2<String, Long>>) (s, collector) -> {
                    String[] words = s.split(" ");
                    for (String word : words) {
                        collector.collect(Tuple2.of(word, 1L));
                    }
                })
                .returns(Types.TUPLE(Types.STRING, Types.LONG)) // TODO 泛型擦除  显示声明返回的类型
                .keyBy(0)
                .sum(1)
                .print();

        env.execute();
    }
}
